---
title: "Python eval test…"
date: 2018-11-21
categories:
- python
tags:
---

<div id="content">
<div class="org-src-container">
<pre class="src src-py">#! python3
expr = 'function(a, b)'

def add(a, b):
    return a+b

val = eval(expr, None, {"function": add, "a": 2, "b": 100})
print(val) # 102

expr = 'function(a, R["NUMBERS"]["HUNDRED"])'
local_var = {}
local_var["function"] = add
local_var["a"] = 3
local_var["R"] = {
    "NUMBERS": {
	"HUNDRED": 100
    }
}
val = eval(expr, None, local_var)
print(val)

expr = "os.getcwd()"
import os
val = eval(expr, None, {"os": os})
print(val)
</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2018-11-21</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
